"""
Verify Review Service Fixed - Test simple_review after fix

Tests the complete review service after fixing the 403 error
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from dotenv import load_dotenv
load_dotenv()

from src.services.review_service import review_service


def test_simple_review():
    """Test simple review endpoint"""

    print("=" * 70)
    print("Test Review Service - Simple Review (Fixed)")
    print("=" * 70)

    # Test data
    user_id = "test_student_001"
    question = "What is photosynthesis and why is it important?"
    grading_criteria = """Grading Criteria (Total 100 points):
- Content Accuracy (40 points): Scientific correctness and understanding
- Completeness (30 points): Coverage of key concepts
- Language Quality (20 points): Grammar and clarity
- Organization (10 points): Structure and flow
"""
    student_answer = """Photosynthesis is the process by which plants make food from sunlight.

Plants use their leaves to capture sunlight. They also need water and carbon dioxide from the air. Through photosynthesis, plants create glucose which they use for energy. Oxygen is released as a byproduct, which is important for animals and humans to breathe.

This process is very important because it provides food for the plant and oxygen for other living things. Without photosynthesis, life on Earth would not be possible."""

    print("\n1. Test Data:")
    print(f"   User ID: {user_id}")
    print(f"   Question: {question}")
    print(f"   Grading Criteria: [Provided]")
    print(f"   Student Answer Length: {len(student_answer)} characters")

    # Call simple_review
    print("\n2. Calling simple_review...")
    try:
        success, result, error = review_service.simple_review(
            user_id=user_id,
            question=question,
            grading_criteria=grading_criteria,
            student_answer=student_answer,
            student_answer_file=None,
            document_name="photosynthesis_answer.txt"
        )

        if success and result:
            print(f"\n[SUCCESS] Review completed!")
            print(f"\n   Review ID: {result.review_id}")
            print(f"   Session ID: {result.session_id}")
            print(f"   Processing Time: {result.processing_time:.2f}s")

            print(f"\n3. Feedback:")
            feedback = result.feedback

            if feedback.overall_grade:
                print(f"   Overall Grade: {feedback.overall_grade}/100")

            print(f"\n   Detailed Feedback:")
            print(f"   {'-' * 66}")
            # Print first 500 chars of feedback
            feedback_preview = feedback.detailed_feedback[:500]
            print(f"   {feedback_preview}...")
            print(f"   {'-' * 66}")

            print(f"\n   Full feedback length: {len(feedback.detailed_feedback)} characters")

            return True

        else:
            print(f"\n[FAILED] Review failed!")
            print(f"   Error: {error}")
            return False

    except Exception as e:
        print(f"\n[FAILED] Exception occurred: {e}")
        import traceback
        traceback.print_exc()
        return False


if __name__ == "__main__":
    success = test_simple_review()

    print("\n" + "=" * 70)
    if success:
        print("[SUCCESS] Review service test passed!")
    else:
        print("[FAILED] Review service test failed!")
    print("=" * 70)

    sys.exit(0 if success else 1)
